//
//  PCDApiInitPageTitle.m
//  PCDBank
//
//  Created by DK on 2021/2/26.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDApiInitPageTitle.h"
#import "PCDApiNavigateDo.h"
#import "PCDBankWebViewController.h"

#define LEFTCALLBACK @"$Fw.call(_Page.onNavigateBack,$appRef.appRouter)"

@interface PCDApiInitPageTitle()
//@property(nonatomic,retain) PCDContext *apiContext;

@end

@implementation PCDApiInitPageTitle

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback
{
    NSDictionary *tempDic = (NSDictionary *)data;
    PCDBankWebViewController *viewPage = context.viewController;
    [viewPage initPageTitleWith:tempDic];
    
//    self.apiContext = context;
//    NSDictionary *tempDic = (NSDictionary *)data;
//    NSString *callBack = [tempDic objectForKey:@"success"];
//
//    PCDApiNavigateDo *navigateDo = [[PCDApiNavigateDo alloc] initWithDict:data];
//    NSString *navigationBarTextSize = navigateDo.navigationBarTextSize;
//    if (![PCDUtil nilOrEmptyString:navigationBarTextSize]) {
//        if ([navigationBarTextSize containsString:@"px"]) {
//            navigationBarTextSize = [navigationBarTextSize stringByReplacingOccurrencesOfString:@"px" withString:@""];
//        }
//    }
//    NSInteger barTextSize = [navigationBarTextSize intValue];
//
//    if (barTextSize ==0) {
//        barTextSize = 18;
//    }
//
//    PCDBankWebViewController *viewPage = context.viewController;
//
//    PCDBarItem *titleItem =[[PCDBarItem alloc] init];
//    titleItem.title = navigateDo.navigationBarTitleText;
//    titleItem.titleColor = [UIColor colorWithHex:0x333333];
//    titleItem.font = [UIFont fontWithName:@"PingFangSC-Medium" size:18.0];
//    viewPage.navigationBar.titleItem = titleItem;
//
//    //默认背景图片设置
//
//    NSString *navigationBarBackgroundColor = navigateDo.navigationBarBackgroundColor;
//    if ([PCDUtil nilOrEmptyString:navigationBarBackgroundColor]) {
//
//        navigationBarBackgroundColor = @"#ffffff";
//        viewPage.navigationBar.backgroundImage = nil;
//
//        PCDBarButtonItem *backBarButtonItem = viewPage.navigationBar.leftBarButtonItem;
//        UIImage *backImage =[UIImage imageNamed:@"取消"];
//        backBarButtonItem.target = viewPage;
//        backBarButtonItem.image = backImage;
//        backBarButtonItem.action = @selector(backBarButtonPressed:);
//
//        viewPage.navigationBar.leftBarButtonItem = backBarButtonItem;
//
//
//    }else{
//        viewPage.navigationBar.backgroundImage = nil;
//        PCDBarButtonItem *backBarButtonItem = viewPage.navigationBar.leftBarButtonItem;
//        UIImage *backImage =[UIImage imageNamed:@"backWhite"];
//        backBarButtonItem.image = backImage;
//        backBarButtonItem.target = viewPage;
//        viewPage.navigationBar.leftBarButtonItem = backBarButtonItem;
//    }
//
//    viewPage.navigationBar.backgroundColor = [UIColor colorFormString:navigationBarBackgroundColor];
//
//    //导航栏类型
//    NSString *navigationStyle = navigateDo.navigationStyle;
//
//    if ([navigationStyle isEqual:NavigationStyleCustom]) {
//
////        [viewPage setNavigationBarHidden:YES animated:NO];
//
//    }else if ([navigationStyle isEqual:NavigationStyleFloat])
//    {
//        //悬浮框
//
//    }else{
//
//        //
//    }
//
//    if (![PCDUtil nilOrEmptyString:callBack]) {
//        //
//        evaluateJavaScriptOnMainThread(self,callBack,@"");
//    }
    
}

-(void)backBarButtonPressed:(PCDBarButtonItem *)sender
{
    WKWebView *webView =self.bsContext.webView;
    
    [webView evaluateJavaScript:LEFTCALLBACK completionHandler:^(id _Nullable ret, NSError * _Nullable error)
    {
        BOOL bCheck = [ret boolValue];
        if ([PCDUtil nilOrEmptyString:ret] && bCheck) {
            [self viewPageBack];
        }
    }];
}

-(void) viewPageBack
{
    NSMutableArray *viewCtrs = [NSMutableArray arrayWithArray:PCDPageViewMangeGet().baseNavgatView.viewControllers];
    
    if (viewCtrs.count>=2) {
        
        id tempView1 = [viewCtrs lastObject];
        id tempView2 = [viewCtrs objectAtIndex:viewCtrs.count -2];
        
        if ([tempView1 isKindOfClass:[PCDBankWebViewController class]] && [tempView2 isKindOfClass:[PCDBankWebViewController class]])
        {
            PCDBankWebViewController *tempPage2 = tempView2;
            PCDPageViewMangeGet().appId = tempPage2.appId;
        }
    }
    PCDBankWebViewController *pageView = (PCDBankWebViewController *)self.bsContext.viewController;
    
    if (pageView.backBlock) {
        pageView.backBlock(YES);
    }
    [pageView.navigationController popViewControllerAnimated:YES];
    
}

@end
